package com.aos.erp.schools.rpc.common;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.enums.student.RegionType;
import com.aos.erp.schools.model.common.Region;
import com.aos.erp.schools.model.common.vo.RegionTree;
import com.aos.erp.schools.model.common.vo.RegionVo;
import com.aos.erp.schools.service.common.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 区域字典
 * @author: tanzhiyu
 * @create: 2018-11-09 17:52
 **/
@Api(value = "区域字典", tags = {"区域字典API"})
@RestController("RPCRegionController")
@RequestMapping("/service/captcha")
public class RegionController {
    @Resource
    private RegionService regionService;

    @ApiOperation(value = "获取所有国家")
    @PostMapping("/getRegionCountry")
    public List<Region> getRegionCountry() throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("leveltype", RegionType.country.getValue());
        List<Region> list=regionService.findByParams(map);
        return list;
    }

    @ApiOperation(value = "获取所有省")
    @PostMapping("/getRegionProvince")
    public List<Region>  getRegionProvince() throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("leveltype", RegionType.province.getValue());
        List<Region> list=regionService.findByParams(map);
        return list;
    }

    @ApiOperation(value = "获取城市")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "城市code", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getRegionCity")
    public  List<RegionVo>  getRegionCity(@RequestBody String code) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("leveltype", RegionType.city.getValue());
        map.put("code",code);
        List<RegionVo> list=regionService.findByParamsParentId(map);
        return list;
    }

    @ApiOperation(value = "获取所有区县")
    @PostMapping("/getRegionArea")
    public  List<Region> getRegionArea() throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("leveltype", RegionType.area.getValue());
        List<Region> list=regionService.findByParams(map);
        return list;
    }

    @ApiOperation(value = "根据上一级获取省市区下级区域")
    @PostMapping("/getRegion")
    public List<Region> getRegion(String parentId) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("parentId", parentId);
        List<Region> list=regionService.findByParams(map);
        return list;
    }

    @ApiOperation(value = "根据区域组织树")
    @GetMapping("/getRegionTree")
    public ResultEntity getRegionTree() throws Exception {
        List<RegionTree> regionTree = regionService.selectTree();
        return ResultEntity.success(regionTree);
    }
}
